package cn.funeralobjects.asms.service.impl;

import cn.funeralobjects.asms.entity.ESmsSign;
import cn.funeralobjects.asms.model.BasicInfo;
import cn.funeralobjects.asms.model.SmsSign;
import cn.funeralobjects.asms.repository.ESmsSignRepository;
import cn.funeralobjects.asms.service.SmsSignService;
import cn.funeralobjects.common.jpa.service.JpaCodeService;
import cn.funeralobjects.common.jpa.service.JpaEntityService;
import cn.funeralobjects.common.jpa.service.JpaModifyService;
import cn.funeralobjects.common.jpa.service.JpaNameService;
import cn.funeralobjects.common.repository.CodeRepository;
import cn.funeralobjects.common.repository.EntityRepository;
import cn.funeralobjects.common.repository.NameRepository;
import cn.funeralobjects.common.service.annotataion.CommonService;
import cn.funeralobjects.common.service.exception.DataConflictException;
import cn.funeralobjects.common.service.validate.DataCheck;
import cn.funeralobjects.util.Assert;
import cn.funeralobjects.util.NumberUtils;
import cn.funeralobjects.util.annotation.ArgHasLength;
import cn.funeralobjects.util.annotation.ArgNotZero;
import cn.funeralobjects.util.annotation.AssertArg;
import org.springframework.data.repository.CrudRepository;

import javax.annotation.Resource;

/**
 * @author FuneralObjects
 * Create date: 2020/5/31 9:33 PM
 */
@CommonService
public class SmsSignServiceImpl implements SmsSignService, JpaEntityService<ESmsSign, Integer>, JpaNameService<ESmsSign, Integer>, JpaModifyService<SmsSign, ESmsSign, Integer>, JpaCodeService<ESmsSign, Integer> {

    @Resource
    private ESmsSignRepository eSmsSignRepository;

    @AssertArg
    @Override
    public void checkConflict(@AssertArg SmsSign smsSign) throws DataConflictException {
        DataCheck.conflictData(this.existsBySignNameInProductRes(smsSign.getProdResCode(), smsSign.getSignName()), () -> createDataCode("prodResCode-signName", smsSign.getProdResCode() + "-" + smsSign.getSignName()));
    }

    @Override
    public ESmsSign toEntity(SmsSign smsSign) {
        return smsSign == null ? null : new ESmsSign()
                .setStatus(smsSign.getStatus())
                .setSignName(smsSign.getSignName())
                .setCode(smsSign.getCode())
                .setReason(smsSign.getReason())
                .setProdResCode(smsSign.getProdResCode())
                .setCreateDate(smsSign.getCreateDate());
    }

    @Override
    public EntityRepository<ESmsSign, Integer> entityRepository() {
        return eSmsSignRepository;
    }

    @Override
    public CrudRepository<ESmsSign, Integer> modifyRepository() {
        return eSmsSignRepository;
    }

    @Override
    public NameRepository<ESmsSign, Integer> nameRepository() {
        return eSmsSignRepository;
    }

    @AssertArg
    @Override
    public void checkConflict(@AssertArg SmsSign smsSign, @ArgNotZero Integer withoutId) throws DataConflictException {
        DataCheck.conflictData(this.existsBySignNameInProductRes(smsSign.getProdResCode(), smsSign.getSignName(), withoutId), () -> createDataCode("prodResCode-signName", smsSign.getProdResCode() + "-" + smsSign.getSignName()));
    }

    @Override
    public void copyToEntity(SmsSign sourceModify, ESmsSign targetEntity) {
        targetEntity.setCreateDate(sourceModify.getCreateDate())
                .setProdResCode(sourceModify.getProdResCode())
                .setCode(sourceModify.getCode())
                .setReason(sourceModify.getReason())
                .setSignName(sourceModify.getSignName())
                .setStatus(sourceModify.getStatus());
    }

    @Override
    public boolean isValid(Integer id) {
        return NumberUtils.notZero(id);
    }

    @Override
    public void validateModify(SmsSign smsSign) {
        Assert.argAssert(smsSign, "smsSign");
    }

    @Override
    public String getName() {
        return ENTITY_NAME;
    }

    @Override
    public String getModuleName() {
        return BasicInfo.MODULE_NAME;
    }

    @AssertArg
    @Override
    public boolean existsBySignNameInProductRes(@ArgHasLength String signName, @ArgHasLength String prodResCode) {
        return eSmsSignRepository.existsAllByProdResCodeAndSignName(prodResCode, signName);
    }

    @AssertArg
    @Override
    public boolean existsBySignNameInProductRes(@ArgHasLength String signName, @ArgHasLength String prodResCode, @ArgNotZero Integer withoutId) {
        return eSmsSignRepository.existsAllByProdResCodeAndSignNameAndIdNot(prodResCode, signName, withoutId);
    }

    @Override
    public CodeRepository<ESmsSign, Integer> codeRepository() {
        return eSmsSignRepository;
    }
}
